﻿#include "OiPCH.hpp"

namespace Oi { namespace Appl {

    namespace
    {
        static QStringList resolve(QString folder, QStringList files)
        {
            QStringList paths;
            for (auto& file : files)
            {
                paths << folder + "\\" + file;
            }
            return paths;
        }
    }

    class SliceBalancePackerImpl : public Impl<SliceBalancePacker>
    {
    public:
        SliceBalancePackerImpl()
        {
        }

    public:

    public:
        void init();
    };

    void SliceBalancePackerImpl::init()
    {
    }

    SliceBalancePacker::SliceBalancePacker()
    {
        OI_I(SliceBalancePacker)->init();
    }

    SliceBalancePacker::~SliceBalancePacker()
    {
        OI_E(SliceBalancePacker);
    }

    int SliceBalancePacker::execute(int argc, char* argv[])
    {
        OiWarning() << "SliceBalancePacker->execute";

        QCoreApplication app(argc, argv);

        QLibrary("OiOperators.dll").load();

        QStringList packages;

        for (const QFileInfo& entry :QDir("packages").entryInfoList(QDir::Dirs | QDir::NoDot | QDir::NoDotDot))
        {
            if (QFile::exists(entry.absoluteFilePath() + "\\" + entry.fileName() + ".dll"))
            {
                packages << entry.absoluteFilePath();
            }
        }

        if (!packages.empty())
        {
            qputenv("PATH", (QProcessEnvironment::systemEnvironment().value("PATH") + ";" + packages.join(";")).toLocal8Bit());
        }

        for (const QFileInfo& entry : QDir("packages").entryInfoList(QDir::Dirs | QDir::NoDot | QDir::NoDotDot))
        {
            if (QFile::exists(entry.absoluteFilePath() + "\\" + entry.fileName() + ".dll"))
            {
                QLibrary(entry.absoluteFilePath() + "\\" + entry.fileName() + ".dll").load();
            }
        }

        int site = -1;

        QString folder;

        for (int i = 0; i < argc; i++)
        {
            if (strcmp(argv[i], "/folder") == 0 && i + 1 < argc)
            {
                folder = QString::fromLocal8Bit(argv[i + 1]);
            }
            if (strcmp(argv[i], "/site") == 0 && i + 1 < argc)
            {
                site = QByteArray(argv[i + 1]).toInt();
            }
        }

        OiWarning() << "Enter Packing: " << folder;

        if (site == 0 || site == 1)
        {
            QList<QSharedPointer<Flow::Environment>> envs;
            envs << QSharedPointer<Flow::Environment>();
            envs << QSharedPointer<Flow::Environment>();
            envs << QSharedPointer<Flow::Environment>();
            envs << QSharedPointer<Flow::Environment>();
            envs << QSharedPointer<Flow::Environment>();
            if (QFile::exists(folder + "\\01_01_DS1.oif"))
            {
                envs[0] = QSharedPointer<Flow::Environment>(new Flow::Environment("DS1"));
                if (envs[0]->load(folder + "\\01_01_DS1.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[0]->program()->find("光图相机_0")->params();
                    params << envs[0]->program()->find("光图相机_1")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\DS1"))
                    {
                        auto files00 = QDir(folder + "\\DS1").entryInfoList(QStringList() << "Site01_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\DS1").entryInfoList(QStringList() << "Site01_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        
                        QMap<qint64, Base::Tuple<QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site01_00_" + QUuid::createUuid().toString() + "_DS1.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site01_01_" + QUuid::createUuid().toString() + "_DS1.mat");
                        }

                        pixmapsList << resolve(folder + "\\DS1", pixmaps00);
                        pixmapsList << resolve(folder + "\\DS1", pixmaps01);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }
                    
                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 2; i++)
                    {
                        envs[0]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>((Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8()))));
                    }
                }
            }
            if (QFile::exists(folder + "\\01_02_DS2.oif"))
            {
                envs[1] = QSharedPointer<Flow::Environment>(new Flow::Environment("DS2"));
                if (envs[1]->load(folder + "\\01_02_DS2.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[1]->program()->find("光图相机_0")->params();
                    params << envs[1]->program()->find("光图相机_1")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\DS2"))
                    {
                        auto files00 = QDir(folder + "\\DS2").entryInfoList(QStringList() << "Site01_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\DS2").entryInfoList(QStringList() << "Site01_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);

                        QMap<qint64, Base::Tuple<QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site01_00_" + QUuid::createUuid().toString() + "_DS2.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site01_01_" + QUuid::createUuid().toString() + "_DS2.mat");
                        }

                        pixmapsList << resolve(folder + "\\DS2", pixmaps00);
                        pixmapsList << resolve(folder + "\\DS2", pixmaps01);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }

                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 2; i++)
                    {
                        envs[1]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>((Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8()))));
                    }
                }
            }
            if (QFile::exists(folder + "\\01_03_TM.oif"))
            {
                envs[2] = QSharedPointer<Flow::Environment>(new Flow::Environment("TM"));
                if (envs[2]->load(folder + "\\01_03_TM.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[2]->program()->find("光图相机_0")->params();
                    params << envs[2]->program()->find("光图相机_1")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\TM"))
                    {
                        auto files00 = QDir(folder + "\\TM").entryInfoList(QStringList() << "Site01_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\TM").entryInfoList(QStringList() << "Site01_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);

                        QMap<qint64, Base::Tuple<QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site01_00_" + QUuid::createUuid().toString() + "_TM.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site01_01_" + QUuid::createUuid().toString() + "_TM.mat");
                        }

                        pixmapsList << resolve(folder + "\\TM", pixmaps00);
                        pixmapsList << resolve(folder + "\\TM", pixmaps01);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }

                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 2; i++)
                    {
                        envs[2]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>((Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8()))));
                    }
                }
            }
            if (QFile::exists(folder + "\\01_AC.oif"))
            {
                envs[3] = QSharedPointer<Flow::Environment>(new Flow::Environment("AC"));
                if (envs[3]->load(folder + "\\01_AC.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[3]->program()->find("光图相机_0")->params();
                    params << envs[3]->program()->find("光图相机_1")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\AC"))
                    {
                        auto files00 = QDir(folder + "\\AC").entryInfoList(QStringList() << "Site01_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\AC").entryInfoList(QStringList() << "Site01_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);

                        QMap<qint64, Base::Tuple<QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site01_00_" + QUuid::createUuid().toString() + "_AC.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site01_01_" + QUuid::createUuid().toString() + "_AC.mat");
                        }

                        pixmapsList << resolve(folder + "\\AC", pixmaps00);
                        pixmapsList << resolve(folder + "\\AC", pixmaps01);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }

                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 2; i++)
                    {
                        envs[3]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>((Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8()))));
                    }
                }
            }
            if (QFile::exists(folder + "\\01_AV.oif"))
            {
                envs[4] = QSharedPointer<Flow::Environment>(new Flow::Environment("AV"));
                if (envs[4]->load(folder + "\\01_AV.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[4]->program()->find("光图相机_0")->params();
                    params << envs[4]->program()->find("光图相机_1")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\AV"))
                    {
                        auto files00 = QDir(folder + "\\AV").entryInfoList(QStringList() << "Site01_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\AV").entryInfoList(QStringList() << "Site01_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);

                        QMap<qint64, Base::Tuple<QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site01_00_" + QUuid::createUuid().toString() + "_AV.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site01_01_" + QUuid::createUuid().toString() + "_AV.mat");
                        }

                        pixmapsList << resolve(folder + "\\AV", pixmaps00);
                        pixmapsList << resolve(folder + "\\AV", pixmaps01);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }

                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 2; i++)
                    {
                        envs[4]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>((Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8()))));
                    }
                }
            }
            if (envs[0])
            {
                envs[0]->save(folder + "\\01_01_DS1.oif");
            }
            if (envs[1])
            {
                envs[1]->save(folder + "\\01_02_DS2.oif");
            }
            if (envs[2])
            {
                envs[2]->save(folder + "\\01_03_TM.oif");
            }
            if (envs[3])
            {
                envs[3]->save(folder + "\\01_AC.oif");
            }
            if (envs[4])
            {
                envs[4]->save(folder + "\\01_AV.oif");
            }
        }
        if (site == 0 || site == 2)
        {
            QList<QSharedPointer<Flow::Environment>> envs;
            envs << QSharedPointer<Flow::Environment>();
            envs << QSharedPointer<Flow::Environment>();
            envs << QSharedPointer<Flow::Environment>();
            envs << QSharedPointer<Flow::Environment>();
            envs << QSharedPointer<Flow::Environment>();
            if (QFile::exists(folder + "\\02_01_PA.oif"))
            {
                envs[0] = QSharedPointer<Flow::Environment>(new Flow::Environment("PA"));
                if (envs[0]->load(folder + "\\02_01_PA.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[0]->program()->find("光图相机_0")->params();
                    params << envs[0]->program()->find("光图相机_1")->params();
                    params << envs[0]->program()->find("光图相机_2")->params();
                    params << envs[0]->program()->find("光图相机_3")->params();
                    params << envs[0]->program()->find("光图相机_4")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\PA"))
                    {
                        auto files00 = QDir(folder + "\\PA").entryInfoList(QStringList() << "Site02_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\PA").entryInfoList(QStringList() << "Site02_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files02 = QDir(folder + "\\PA").entryInfoList(QStringList() << "Site02_02*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files03 = QDir(folder + "\\PA").entryInfoList(QStringList() << "Site02_03*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files04 = QDir(folder + "\\PA").entryInfoList(QStringList() << "Site02_04*.mat", QDir::Files, QDir::Reversed | QDir::Time);

                        QMap<qint64, Base::Tuple<QString, QString, QString, QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files02)
                        {
                            std::get<2>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files03)
                        {
                            std::get<3>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files04)
                        {
                            std::get<4>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;
                        QStringList pixmaps02;
                        QStringList pixmaps03;
                        QStringList pixmaps04;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            QString item02 = std::get<2>(item);
                            QString item03 = std::get<3>(item);
                            QString item04 = std::get<4>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site02_00_" + QUuid::createUuid().toString() + "_PA.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site02_01_" + QUuid::createUuid().toString() + "_PA.mat");
                            pixmaps02 << (!item02.isEmpty() ? item02 : "Site02_02_" + QUuid::createUuid().toString() + "_PA.mat");
                            pixmaps03 << (!item03.isEmpty() ? item03 : "Site02_03_" + QUuid::createUuid().toString() + "_PA.mat");
                            pixmaps04 << (!item04.isEmpty() ? item04 : "Site02_04_" + QUuid::createUuid().toString() + "_PA.mat");
                        }

                        pixmapsList << resolve(folder + "\\PA", pixmaps00);
                        pixmapsList << resolve(folder + "\\PA", pixmaps01);
                        pixmapsList << resolve(folder + "\\PA", pixmaps02);
                        pixmapsList << resolve(folder + "\\PA", pixmaps03);
                        pixmapsList << resolve(folder + "\\PA", pixmaps04);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }

                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 5; i++)
                    {
                        envs[0]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>(Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8())));
                    }
                }
            }
            if (QFile::exists(folder + "\\02_02_BP1.oif"))
            {
                envs[1] = QSharedPointer<Flow::Environment>(new Flow::Environment("BP1"));
                if (envs[1]->load(folder + "\\02_02_BP1.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[1]->program()->find("光图相机_0")->params();
                    params << envs[1]->program()->find("光图相机_1")->params();
                    params << envs[1]->program()->find("光图相机_2")->params();
                    params << envs[1]->program()->find("光图相机_3")->params();
                    params << envs[1]->program()->find("光图相机_4")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\BP1"))
                    {
                        auto files00 = QDir(folder + "\\BP1").entryInfoList(QStringList() << "Site02_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\BP1").entryInfoList(QStringList() << "Site02_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files02 = QDir(folder + "\\BP1").entryInfoList(QStringList() << "Site02_02*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files03 = QDir(folder + "\\BP1").entryInfoList(QStringList() << "Site02_03*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files04 = QDir(folder + "\\BP1").entryInfoList(QStringList() << "Site02_04*.mat", QDir::Files, QDir::Reversed | QDir::Time);

                        QMap<qint64, Base::Tuple<QString, QString, QString, QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files02)
                        {
                            std::get<2>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files03)
                        {
                            std::get<3>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files04)
                        {
                            std::get<4>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;
                        QStringList pixmaps02;
                        QStringList pixmaps03;
                        QStringList pixmaps04;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            QString item02 = std::get<2>(item);
                            QString item03 = std::get<3>(item);
                            QString item04 = std::get<4>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site02_00_" + QUuid::createUuid().toString() + "_BP1.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site02_01_" + QUuid::createUuid().toString() + "_BP1.mat");
                            pixmaps02 << (!item02.isEmpty() ? item02 : "Site02_02_" + QUuid::createUuid().toString() + "_BP1.mat");
                            pixmaps03 << (!item03.isEmpty() ? item03 : "Site02_03_" + QUuid::createUuid().toString() + "_BP1.mat");
                            pixmaps04 << (!item04.isEmpty() ? item04 : "Site02_04_" + QUuid::createUuid().toString() + "_BP1.mat");
                        }

                        pixmapsList << resolve(folder + "\\BP1", pixmaps00);
                        pixmapsList << resolve(folder + "\\BP1", pixmaps01);
                        pixmapsList << resolve(folder + "\\BP1", pixmaps02);
                        pixmapsList << resolve(folder + "\\BP1", pixmaps03);
                        pixmapsList << resolve(folder + "\\BP1", pixmaps04);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }

                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 5; i++)
                    {
                        envs[1]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>((Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8()))));
                    }
                }
            }
            if (QFile::exists(folder + "\\02_03_BP2.oif"))
            {
                envs[2] = QSharedPointer<Flow::Environment>(new Flow::Environment("BP2"));
                if (envs[2]->load(folder + "\\02_03_BP2.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[2]->program()->find("光图相机_0")->params();
                    params << envs[2]->program()->find("光图相机_1")->params();
                    params << envs[2]->program()->find("光图相机_2")->params();
                    params << envs[2]->program()->find("光图相机_3")->params();
                    params << envs[2]->program()->find("光图相机_4")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\BP2"))
                    {
                        auto files00 = QDir(folder + "\\BP2").entryInfoList(QStringList() << "Site02_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\BP2").entryInfoList(QStringList() << "Site02_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files02 = QDir(folder + "\\BP2").entryInfoList(QStringList() << "Site02_02*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files03 = QDir(folder + "\\BP2").entryInfoList(QStringList() << "Site02_03*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files04 = QDir(folder + "\\BP2").entryInfoList(QStringList() << "Site02_04*.mat", QDir::Files, QDir::Reversed | QDir::Time);

                        QMap<qint64, Base::Tuple<QString, QString, QString, QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files02)
                        {
                            std::get<2>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files03)
                        {
                            std::get<3>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files04)
                        {
                            std::get<4>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;
                        QStringList pixmaps02;
                        QStringList pixmaps03;
                        QStringList pixmaps04;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            QString item02 = std::get<2>(item);
                            QString item03 = std::get<3>(item);
                            QString item04 = std::get<4>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site02_00_" + QUuid::createUuid().toString() + "_BP2.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site02_01_" + QUuid::createUuid().toString() + "_BP2.mat");
                            pixmaps02 << (!item02.isEmpty() ? item02 : "Site02_02_" + QUuid::createUuid().toString() + "_BP2.mat");
                            pixmaps03 << (!item03.isEmpty() ? item03 : "Site02_03_" + QUuid::createUuid().toString() + "_BP2.mat");
                            pixmaps04 << (!item04.isEmpty() ? item04 : "Site02_04_" + QUuid::createUuid().toString() + "_BP2.mat");
                        }

                        pixmapsList << resolve(folder + "\\BP2", pixmaps00);
                        pixmapsList << resolve(folder + "\\BP2", pixmaps01);
                        pixmapsList << resolve(folder + "\\BP2", pixmaps02);
                        pixmapsList << resolve(folder + "\\BP2", pixmaps03);
                        pixmapsList << resolve(folder + "\\BP2", pixmaps04);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }

                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 5; i++)
                    {
                        envs[2]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>((Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8()))));
                    }
                }
            }
            if (QFile::exists(folder + "\\02_AC.oif"))
            {
                envs[3] = QSharedPointer<Flow::Environment>(new Flow::Environment("AC"));
                if (envs[3]->load(folder + "\\02_AC.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[3]->program()->find("光图相机_0")->params();
                    params << envs[3]->program()->find("光图相机_1")->params();
                    params << envs[3]->program()->find("光图相机_2")->params();
                    params << envs[3]->program()->find("光图相机_3")->params();
                    params << envs[3]->program()->find("光图相机_4")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\AC"))
                    {
                        auto files00 = QDir(folder + "\\AC").entryInfoList(QStringList() << "Site02_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\AC").entryInfoList(QStringList() << "Site02_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files02 = QDir(folder + "\\AC").entryInfoList(QStringList() << "Site02_02*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files03 = QDir(folder + "\\AC").entryInfoList(QStringList() << "Site02_03*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files04 = QDir(folder + "\\AC").entryInfoList(QStringList() << "Site02_04*.mat", QDir::Files, QDir::Reversed | QDir::Time);

                        QMap<qint64, Base::Tuple<QString, QString, QString, QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files02)
                        {
                            std::get<2>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files03)
                        {
                            std::get<3>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files04)
                        {
                            std::get<4>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;
                        QStringList pixmaps02;
                        QStringList pixmaps03;
                        QStringList pixmaps04;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            QString item02 = std::get<2>(item);
                            QString item03 = std::get<3>(item);
                            QString item04 = std::get<4>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site02_00_" + QUuid::createUuid().toString() + "_AC.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site02_01_" + QUuid::createUuid().toString() + "_AC.mat");
                            pixmaps02 << (!item02.isEmpty() ? item02 : "Site02_02_" + QUuid::createUuid().toString() + "_AC.mat");
                            pixmaps03 << (!item03.isEmpty() ? item03 : "Site02_03_" + QUuid::createUuid().toString() + "_AC.mat");
                            pixmaps04 << (!item04.isEmpty() ? item04 : "Site02_04_" + QUuid::createUuid().toString() + "_AC.mat");
                        }

                        pixmapsList << resolve(folder + "\\AC", pixmaps00);
                        pixmapsList << resolve(folder + "\\AC", pixmaps01);
                        pixmapsList << resolve(folder + "\\AC", pixmaps02);
                        pixmapsList << resolve(folder + "\\AC", pixmaps03);
                        pixmapsList << resolve(folder + "\\AC", pixmaps04);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }

                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 5; i++)
                    {
                        envs[3]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>((Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8()))));
                    }
                }
            }
            if (QFile::exists(folder + "\\02_AV.oif"))
            {
                envs[4] = QSharedPointer<Flow::Environment>(new Flow::Environment("AV"));
                if (envs[4]->load(folder + "\\02_AV.oif") == 0)
                {
                    QList<Flow::Params*> params;
                    params << envs[4]->program()->find("光图相机_0")->params();
                    params << envs[4]->program()->find("光图相机_1")->params();
                    params << envs[4]->program()->find("光图相机_2")->params();
                    params << envs[4]->program()->find("光图相机_3")->params();
                    params << envs[4]->program()->find("光图相机_4")->params();

                    QList<QStringList> pixmapsList;
                    if (QFile::exists(folder + "\\AV"))
                    {
                        auto files00 = QDir(folder + "\\AV").entryInfoList(QStringList() << "Site02_00*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files01 = QDir(folder + "\\AV").entryInfoList(QStringList() << "Site02_01*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files02 = QDir(folder + "\\AV").entryInfoList(QStringList() << "Site02_02*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files03 = QDir(folder + "\\AV").entryInfoList(QStringList() << "Site02_03*.mat", QDir::Files, QDir::Reversed | QDir::Time);
                        auto files04 = QDir(folder + "\\AV").entryInfoList(QStringList() << "Site02_04*.mat", QDir::Files, QDir::Reversed | QDir::Time);

                        QMap<qint64, Base::Tuple<QString, QString, QString, QString, QString>> mapper;

                        for (auto& file : files00)
                        {
                            std::get<0>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files01)
                        {
                            std::get<1>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files02)
                        {
                            std::get<2>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files03)
                        {
                            std::get<3>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }
                        for (auto& file : files04)
                        {
                            std::get<4>(mapper[file.lastModified().toMSecsSinceEpoch() / 30 / 1000]) = file.fileName();
                        }

                        QStringList pixmaps00;
                        QStringList pixmaps01;
                        QStringList pixmaps02;
                        QStringList pixmaps03;
                        QStringList pixmaps04;

                        for (auto& item : mapper)
                        {
                            QString item00 = std::get<0>(item);
                            QString item01 = std::get<1>(item);
                            QString item02 = std::get<2>(item);
                            QString item03 = std::get<3>(item);
                            QString item04 = std::get<4>(item);
                            pixmaps00 << (!item00.isEmpty() ? item00 : "Site02_00_" + QUuid::createUuid().toString() + "_AV.mat");
                            pixmaps01 << (!item01.isEmpty() ? item01 : "Site02_01_" + QUuid::createUuid().toString() + "_AV.mat");
                            pixmaps02 << (!item02.isEmpty() ? item02 : "Site02_02_" + QUuid::createUuid().toString() + "_AV.mat");
                            pixmaps03 << (!item03.isEmpty() ? item03 : "Site02_03_" + QUuid::createUuid().toString() + "_AV.mat");
                            pixmaps04 << (!item04.isEmpty() ? item04 : "Site02_04_" + QUuid::createUuid().toString() + "_AV.mat");
                        }

                        pixmapsList << resolve(folder + "\\AV", pixmaps00);
                        pixmapsList << resolve(folder + "\\AV", pixmaps01);
                        pixmapsList << resolve(folder + "\\AV", pixmaps02);
                        pixmapsList << resolve(folder + "\\AV", pixmaps03);
                        pixmapsList << resolve(folder + "\\AV", pixmaps04);
                    }
                    else
                    {
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                        pixmapsList << QStringList();
                    }

                    for (int i = 0; i < params.size(); i++)
                    {
                        params[i]->setProperty("pixmaps", QVariant(pixmapsList[i]));
                    }

                    for (int i = 0; i < 5; i++)
                    {
                        envs[4]->program()->find(QString("刷新视图_%1").arg(i))->params()->setProperty("source", QVariant::fromValue<Core::Refer>((Core::Refer(QString("光图相机_%1.output.rMap").arg(i).toUtf8()))));
                    }
                }
            }
            if (envs[0])
            {
                envs[0]->save(folder + "\\02_01_PA.oif");
            }
            if (envs[1])
            {
                envs[1]->save(folder + "\\02_02_BP1.oif");
            }
            if (envs[2])
            {
                envs[2]->save(folder + "\\02_03_BP2.oif");
            }
            if (envs[3])
            {
                envs[3]->save(folder + "\\02_AC.oif");
            }
            if (envs[4])
            {
                envs[4]->save(folder + "\\02_AV.oif");
            }
        }

        OiWarning() << "Leave Packing";

        return 0;
    }

} }
